package com.stellar.circle.controller.miniApp.payment;


import com.stellar.circle.domain.payment.PaymentDomainService;
import com.stellar.pay.core.entity.PrepayResponse;
import com.stellar.pay.core.service.IWxPayCommonService;
import com.stellar.web.core.bean.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @Author Jqcc
 * @Version 1.0
 * @Data 2025/4/11
 */
@RequestMapping("/api-miniApp/payment")
@RestController("miniAppPaymentController")
@Slf4j
public class PaymentController {

    @Resource
    private PaymentDomainService paymentDomainService;

    @Resource
    private IWxPayCommonService wxPayCommonService;


    /**
     * 提交预支付订单
     *
     * @param pinOrderNo   置顶订单号
     * @param openId       openId
     * @param actualAmount 实际支付金额
     * @param description  描述
     */
    @PostMapping(("/submitPinOrder"))
    public Result<PrepayResponse> submitOrder(@RequestParam("pinOrderNo") String pinOrderNo,
                                              @RequestParam("openid") String openId,
                                              @RequestParam("actualAmount") BigDecimal actualAmount,
                                              @RequestParam("description") String description) {
        log.info("PaymentController.submitOrder.pinOrderNo:{}", pinOrderNo);
        PrepayResponse prepayResponse = paymentDomainService.submitPinOrder(pinOrderNo, openId, actualAmount, description);
        if (Objects.isNull(prepayResponse)) {
            return Result.fail();
        }
        return Result.success(prepayResponse);
    }


    /**
     * 处理支付成功的回调
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/callback")
    public Result callback(HttpServletRequest request, HttpServletResponse response) {
        wxPayCommonService.handlePaymentNotifyBySDK(request, response);
        return Result.success();
    }

}
